package dwei.pagination
{
	import mx.core.UIComponent;
	
	public class PaginationUI extends UIComponent
	{
//============================================================================
//PROPERTY
//============================================================================
		private var _contentChanged:Boolean = false;
		
		protected var _number:uint;
		protected var _current:int;
		protected var _count:int;
		protected const _list:Array = [];
//============================================================================
//CONSTRUCTOR
//============================================================================
		public function PaginationUI()
		{
			super();
			mouseEnabled = false;
			
			_number = 1;
			_count = _current = 0;
		}
//============================================================================
//OVERRIDE
//============================================================================
		override protected function commitProperties():void
		{
			super.commitProperties();
			if (_contentChanged)
			{
				validateContent();
				_contentChanged = false;
			}
		}
		override protected function measure():void
		{
			super.measure();
			measuredWidth = measuredHeight = 0;
			var unit:IPaginationUnit;
			var index:int = numChildren - 1;
			while (index > -1)
			{
				unit = getChildAt(index--) as IPaginationUnit;
				if (unit != null)
				{
					measuredWidth = Math.max(measuredWidth, unit.getExplicitOrMeasuredWidth());
					measuredHeight += unit.getExplicitOrMeasuredHeight();
				}
			}
		}
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			validateLayout(unscaledWidth, unscaledHeight);
		}
//============================================================================
//PUBLIC
//============================================================================
		public function nextPage():void
		{
			current++;
		}
		public function lastPage():void
		{
			current--;
		}
		public function removeAllChildren():void
		{
			while (numChildren > 0)
			{
				removeChildAt(0);
			}
		}
		public function invalidateContent():void
		{
			_contentChanged = true;
			invalidateProperties();
			invalidateSize();
		}
		public function append(unit:IPaginationUnit):void
		{
			var index:int = _list.indexOf(unit);
			if (index != -1) _list.splice(_list.indexOf(unit), 1);
			_list.push(unit);
			_count = Math.ceil(_list.length / _number);
			invalidateContent();
		}
		public function remove(unit:IPaginationUnit):void
		{
			var index:int = _list.indexOf(unit);
			if (index != -1) _list.splice(_list.indexOf(unit), 1);
			_count = Math.ceil(_list.length / _number);
			invalidateContent();
		}
//============================================================================
//PROTECTED
//============================================================================
		/**
		 * 更新容器的显示对象
		 * 
		 */		
		protected function validateContent():void
		{
			var minPage:int = Math.min(_count, 1);
			
			if (_current < minPage)
				_current = minPage;
			else if (_current > _count)
				_current = _count;
			
			removeAllChildren();
			
			if (_current == 0) return;
			var index:int;
			var indexList:int;
			var indexTargetStart:int = (_current - 1) * _number;
			for (index = 0;index < _number;index++)
			{
				indexList = index + indexTargetStart;
				if (indexList < _list.length) addChild(_list[indexList]);
				else break;
			}
			invalidateDisplayList();
		}
		/**
		 * 布局
		 * @param unscaledWidth
		 * @param unscaledHeight
		 * 
		 */		
		protected function validateLayout(unscaledWidth:Number, unscaledHeight:Number):void
		{
			var y:int = 0;
			var unit:IPaginationUnit;
			var length:int = numChildren;
			for (var index:int = 0;index < numChildren;index++)
			{
				unit = getChildAt(index) as IPaginationUnit;
				unit.setActualSize(unit.getExplicitOrMeasuredWidth(), unit.getExplicitOrMeasuredHeight());
				unit.y = y;
				y += unit.height;
			}
		}
//============================================================================
//GETTER/SETTER
//============================================================================

		/**
		 * 每页显示数量
		 */
		public function get number():uint
		{
			return _number;
		}

		/**
		 * @private
		 */
		public function set number(value:uint):void
		{
			if (value == _number) return;
			_number = value;
			invalidateContent();
		}

		/**
		 * 当前页数
		 */
		public function get current():int
		{
			return _current;
		}

		/**
		 * @private
		 */
		public function set current(value:int):void
		{
			if ((value < 1) || (value > _count)) return;
			if (value == _current) return;
			_current = value;
			invalidateContent();
		}

		/**
		 * 总页数
		 */
		public function get count():int
		{
			return _count;
		}

		/**
		 * 内容集合
		 */
		public function get list():Array
		{
			return _list;
		}


	}
}